/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.database.SqlAccount;
import model.entities.Account;

/**
 *
 * @author THANH
 */
public class LoginAction implements Action {

    public void exe(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlAccount sa = new SqlAccount();
        HttpSession session = request.getSession();   
        try {
            String submit = request.getParameter("btnLogin");
            if (submit.equals("Login")) {
                String uname = request.getParameter("txtUname");
                String pass = request.getParameter("txtPass");
                Account account = sa.getAccount(uname);
                if (account != null) {
                    if (account.getPassword().equals(pass)) {
                        request.setAttribute("LogStatus", true);
                        session.setAttribute("Account", account);
                    }
                } else {
                    request.setAttribute("LogStatus", false);
                }
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
